unit GlavnaKnjigaRDM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,
  DBClient, StdVcl, FMTBcd, Provider, alProvider, DB, SqlExpr,
  PoslovanjeSrv_TLB,
  DokumentRDM;

type
  TrdmGlavnaKnjiga = class(TrdmDokument, IrdmGlavnaKnjiga)
  private
    { Private declarations }
  protected
    class procedure UpdateRegistry(Register: Boolean; const ClassID, ProgID: string); override;
    function GetSelectCommandText(DataSet: TSQLDataSet): string; override;
    function Get_RazredID(const ProviderName: WideString): Integer; safecall;
    function GetTableName: string; override;
    procedure RefreshRecord(const Session: ISession;
      const ProviderName: WideString; ID: Integer); safecall;
    procedure Renumber(const Session: ISession; const FieldName,
      SortFieldName: WideString; StartValue, EndValue: Integer); safecall;
    procedure SetDefaultExpression(DataSet: TDataSet); override;
    procedure Set_ReadOnly(Value: WordBool); safecall;
    procedure Set_SQLConnection(Value: Integer); safecall;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  GlavnaKnjigaFactory: TComponentFactory;

implementation

{$R *.DFM}

uses
  DokumentStatus;

constructor TrdmGlavnaKnjiga.Create(AOwner: TComponent);
begin
  inherited;
  dspMain.Options := dspMain.Options + [poReadOnly];
end;

function TrdmGlavnaKnjiga.GetSelectCommandText(
  DataSet: TSQLDataSet): string;
var
  SQL: TStringList;
begin
  SQL := TStringList.Create;
  try
    if DataSet = dsMain then
    begin
      SQL.Add('select');
        SQL.Add('TPO.ID,');
        SQL.Add('TEM.RAZRED__ID,');
        SQL.Add('TEM.REF_ID,');
        SQL.Add('TEM.DOKUMENT_ID,');
        SQL.Add('TEM.STEVILKA,');
        SQL.Add('TEM.DATUM,');
        SQL.Add('STE.OZNAKA as SKUPINA_OZNAKA,');
        SQL.Add('TEM.OPOMBA,');
        SQL.Add('TEM.STATUS,');
        SQL.Add('substring(KON.SIFRA from 1 for 1) as POSTAVKA_KONTO_1,');
        SQL.Add('substring(KON.SIFRA from 1 for 2) as POSTAVKA_KONTO_2,');
        SQL.Add('substring(KON.SIFRA from 1 for 3) as POSTAVKA_KONTO_3,');
        SQL.Add('substring(KON.SIFRA from 1 for 4) as POSTAVKA_KONTO_4,');
        SQL.Add('KON.SIFRA as POSTAVKA_KONTO_SIFRA,');
        SQL.Add('SIF.NAZIV as POSTAVKA_SIFRANT_NAZIV,');
        SQL.Add('TPS.STEVILKA as POSTAVKA_TEMELJNICA_STEVILKA,');
        SQL.Add('TPO.DEBET as POSTAVKA_DEBET,');
        SQL.Add('TPO.KREDIT as POSTAVKA_KREDIT,');
        SQL.Add('TPO.OPOMBA as POSTAVKA_OPOMBA');
      SQL.Add('from TEMELJNICA_POSTAVKA TPO');
      SQL.Add('inner join TEMELJNICA TEM on TPO.REF_ID = TEM.ID');
      SQL.Add('inner join SKUPINA_TEMELJNICA STE on (TEM.SKUPINA_ID = STE.ID)');
      SQL.Add('inner join KONTO KON on (TPO.KONTO_ID = KON.ID)');
      SQL.Add('left outer join SIFRANT SIF on (TPO.SIFRANT_ID = SIF.ID)');
      SQL.Add('left outer join TPO_GET_STEVILKA(TPO.POSTAVKA_ID) TPS on (TPO.POSTAVKA_ID = TPS.POSTAVKA_ID)');
      SQL.Add('where');
        SQL.Add('(TEM.REF_ID = :POSLOVNO_LETO_ID) and');
        SQL.Add(Format('(BIN_AND(TEM.STATUS, %0:d) = %0:d)', [1 shl (stTemeljnicaPrenosGKBit - 1)]));
    end;
  finally
    Result := Trim(SQL.Text);
    SQL.Free;
  end;
end;

function TrdmGlavnaKnjiga.GetTableName: string;
begin
  Result := 'TPO';
end;

function TrdmGlavnaKnjiga.Get_RazredID(
  const ProviderName: WideString): Integer;
begin
  Result := -1;
end;

procedure TrdmGlavnaKnjiga.RefreshRecord(const Session: ISession;
  const ProviderName: WideString; ID: Integer);
begin
  inherited;
end;

procedure TrdmGlavnaKnjiga.Renumber(const Session: ISession;
  const FieldName, SortFieldName: WideString; StartValue,
  EndValue: Integer);
begin
  inherited;
end;

procedure TrdmGlavnaKnjiga.SetDefaultExpression(DataSet: TDataSet);
begin

end;

procedure TrdmGlavnaKnjiga.Set_ReadOnly(Value: WordBool);
begin
  inherited;
end;

procedure TrdmGlavnaKnjiga.Set_SQLConnection(Value: Integer);
begin
  inherited;
end;

class procedure TrdmGlavnaKnjiga.UpdateRegistry(Register: Boolean; const ClassID, ProgID: string);
begin
  if Register then
  begin
    inherited UpdateRegistry(Register, ClassID, ProgID);
    EnableSocketTransport(ClassID);
    EnableWebTransport(ClassID);
  end else
  begin
    DisableSocketTransport(ClassID);
    DisableWebTransport(ClassID);
    inherited UpdateRegistry(Register, ClassID, ProgID);
  end;
end;

initialization
  GlavnaKnjigaFactory := TComponentFactory.Create(ComServer, TrdmGlavnaKnjiga,
    Class_rdmGlavnaKnjiga, ciInternal, tmApartment);
end.
